layui.use(['layer', 'form', 'table', 'admin', 'ax'], function () {
    var $ = layui.$;
    var layer = layui.layer;
    var form = layui.form;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;


    /**
     * 定时任务日志管理初始化
     */
    var SysJobLog = {
        tableId: "SysJobLogTable",	//表格id
        condition: {}
    };

    /**
     * 初始化表格的列
     */
    SysJobLog.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {title: '任务名称', field: 'jobName', visible: true, align: 'center', valign: 'middle'},
            {title: '任务组名', field: 'jobGroup', visible: true, align: 'center', valign: 'middle'},
            {title: '调用目标字符串', field: 'invokeTarget', visible: true, align: 'center', valign: 'middle'},
            {title: '日志信息', field: 'jobMessage', visible: true, align: 'center', valign: 'middle'},
            {
                title: '状态',
                field: 'status',
                visible: true,
                align: 'center',
                valign: 'middle',
                templet: function (data) {
                    if (data.status == 0) {
                        return '<span class="layui-btn layui-btn-xs layui-btn-radius layui-bg-green">正常</span>';
                    } else {
                        return '<span class="layui-btn layui-btn-xs layui-btn-radius layui-bg-red">异常</span>';
                    }
                }
            },
            {title: '创建时间', field: 'createTime', visible: true, align: 'center', valign: 'middle'},
            {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };


    /**
     * 打开查看定时任务日志详情
     */
    SysJobLog.openSysJobLogDetail = function (data) {
        top.layui.admin.open({
            type: 2,
            title: '详情',
            area: ['600px', '650px'],
            content: Feng.ctxPath + '/sysJobLog/sysJobLog_info?sysJobLogId=' + data.jobLogId,
            end: function () {
            }
        });
    };

    /**
     * 删除定时任务日志
     */
    SysJobLog.delete = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/sysJobLog/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(SysJobLog.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            }, true);
            ajax.start();
        }
        Feng.confirm("确定清空所有调度日志吗?", operation);
    };


    var sysJobId = $("#sysJobId").val();
    // 渲染表格
    var tableResult = table.render({
        elem: '#' + SysJobLog.tableId,
        url: Feng.ctxPath + '/sysJobLog/list',
        page: true,
        height: "full-50",
        cellMinWidth: 100,
        toolbar: "#toolbar",
        cols: SysJobLog.initColumn(),
        where: {"sysJobId": sysJobId}
    });


    //头工具栏事件
    table.on('toolbar(' + SysJobLog.tableId + ')', function(obj){
        switch(obj.event){
            case 'btnClear':
                SysJobLog.delete();
                break;
        };
    });

    // 工具条点击事件
    table.on('tool(' + SysJobLog.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;
        if (layEvent === 'edit') {
            SysJobLog.onEditSysJobLog(data);
        } else if (layEvent === 'delete') {
            SysJobLog.delete(data);
        } else if (layEvent === 'info') {
            SysJobLog.openSysJobLogDetail(data);
        } else if (layEvent === 'info') {
            SysJobLog.openSysJobLogDetail(data);
        }
    });
});

